﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Net.Sz.Framework.ExcelTools.CreateCode.excel.java
{
    /// <summary>
    ///
    /// <para>PS:</para>
    /// <para>@author 失足程序员</para>
    /// <para>@Blog http://www.cnblogs.com/shizuchengxuyuan/</para>
    /// <para>@mail 492794628@qq.com</para>
    /// <para>@phone 13882122019</para>
    /// </summary>
    public class CreateJavaHiJpaCode : CreateBase
    {
        static readonly CreateJavaHiJpaCode instance = new CreateJavaHiJpaCode();
        public static CreateJavaHiJpaCode Instance()
        {
            return instance;
        }

        protected override void CreateClassFile(string fileName, string msg)
        {
            base.CreateClassFile(JavaJpaCodePathData + "/" + fileName + ".java", msg);
        }

        public void ActionExcelData(ExcelDatas exceldatas, string strnamespace, Action<string> show)
        {
            Dictionary<string, ExcelData> datas = exceldatas.Datas;

            List<string> snames = new List<string>();

            HashSet<string> filenames = new HashSet<string>();

            foreach (var data in datas)
            {
                string filename = ExcelData.FirstUpper(data.Value.SheetName.Replace("_", ""));
                if (data.Value.Rows == null || data.Value.Rows.Count == 0 || !filenames.Add(filename))
                {
                    continue;
                }
                CreateClassFile(filename, CreateCode(datas, data.Value, strnamespace, show));
            }
        }

        string CreateCode(Dictionary<string, ExcelData> datas, ExcelData model, string strnamespace, Action<string> show)
        {
            StringBuilder csharpBuilder = new StringBuilder();
            StringBuilder csharpBuilder2 = new StringBuilder();
            string sheetName = model.SheetName;

            csharpBuilder.Append("package ").Append(strnamespace).AppendLine(";")
                .AppendLine()
                .AppendLine()
                .AppendLine("import java.io.Serializable;")
                .AppendLine("import javax.persistence.Column;")
                .AppendLine("import javax.persistence.Entity;")
                .AppendLine("import javax.persistence.Id;")
                .AppendLine("import javax.persistence.Table;")
                .AppendLine("import javax.persistence.MappedSuperclass;")
                .AppendLine()
                .AppendLine("/**")
                .AppendLine("* ")
                .AppendLine("* <br>")
                .AppendLine("* Excel Data To Java Jpa C3P0 Class<br>")
                .AppendLine("* Create Code：失足程序员<br>")
                .AppendLine("* Blog http://www.cnblogs.com/shizuchengxuyuan/<br>")
                .AppendLine("* email：492794628@qq.com<br>")
                .AppendLine("* Phone：13882122019<br>")
                .AppendLine("* Create Time：" + (DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:sss")) + "<br>")
                .AppendLine("*/")
                .AppendLine("@Entity")
                .AppendLine("@Table(name = \"" + sheetName + "" + "\")")
                .AppendLine("@MappedSuperclass")
                .AppendLine("public class Q" + sheetName.Replace("q_", "") + " implements Serializable {")
                .AppendLine();

            foreach (var row in model.Rows)
            {
                var cells = row.Value.Cells;
                foreach (var cell in cells)
                {
                    if ("ALL".Equals(cell.Value.Cells) || "AS".Equals(cell.Value.Cells))
                    {

                        csharpBuilder.Append("     /**").AppendLine();
                        csharpBuilder.Append("     * " + cell.Value.CellNotes).Append((String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : "关联文件：" + cell.Value.CellFileName)).AppendLine();
                        csharpBuilder.Append("     */").AppendLine();
                        if (cell.Value.IsPKey)
                        {
                            csharpBuilder.AppendLine("     @Id");
                        }

                        csharpBuilder.Append("     @Column(name = \"").Append(cell.Value.CellName).AppendLine("\")");
                        csharpBuilder.Append("     protected " + cell.Value.CellValueType + " " + cell.Value.CellName2Coder + ";").AppendLine();

                        csharpBuilder2
                            .AppendLine("     ")
                            .AppendLine("     /**")
                            .AppendLine("     * " + cell.Value.CellNotes + " " + (String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : ("关联文件：" + cell.Value.CellFileName) + "<br>"))
                            .AppendLine("     */")
                            .AppendLine("     public void set" + cell.Value.CellName2CoderUpper + "(" + cell.Value.CellValueType + " " + cell.Value.CellName2Coder + "){")
                            .AppendLine("          this." + cell.Value.CellName2Coder + " = " + cell.Value.CellName2Coder + ";")
                            .AppendLine("     }")
                            .AppendLine("     ")
                            .AppendLine("     /**")
                            .AppendLine("     * " + cell.Value.CellNotes + " " + (String.IsNullOrWhiteSpace(cell.Value.CellFileName) ? "" : ("关联文件：" + cell.Value.CellFileName) + "<br>"))
                            .AppendLine("     */")
                            .AppendLine("     public " + cell.Value.CellValueType + " get" + cell.Value.CellName2CoderUpper + "() {")
                            .AppendLine("          return this." + cell.Value.CellName2Coder + ";")
                            .AppendLine("     }")
                            .AppendLine();

                    }
                }
                break;
            }
            csharpBuilder.Append(csharpBuilder2);
            csharpBuilder.AppendLine("}");// Class End
            return csharpBuilder.ToString();
        }
    }
}

